﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TLCApp.pulic;

namespace TLCApp
{
    public partial class Rent12 : Form
    {
        public Rent12()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            querydata();
        }

        void querydata()
        {
            try
            {
                string strxml = "";
                strxml = "<DocumentElement><SYS_EMPLOYEE><EMPLOYEE_ID>" + Cls_Func.filetrchar(txtEmployeeIdQ.Text) + "</EMPLOYEE_ID>";
                strxml = strxml + "<EMPLOYEE_MC> " + Cls_Func.filetrchar(this.txt_EmployeeNameQ.Text) + "</EMPLOYEE_MC>";
                strxml = strxml + "<DEPTCODE> " + (Cls_Func.filetrchar(cboDepartmentIdQ.SelectedValue.ToString())) + "</DEPTCODE>" + "</SYS_EMPLOYEE></DocumentElement>";
                DataSet dsResult = Dal.sys.Sys_employee_query(strxml);
                dgvlist.DataSource = dsResult.Tables[0];
            }
            catch (Exception ex)
            {

                pulic.Cls_Popup.MsgErrBox( ex.ToString());
            }
        
        
        }

        void setcontrol()
        {
            dgvlist.AutoGenerateColumns = false;
            this.dgvlist.AllowUserToAddRows = false;
            this.dgvlist.MultiSelect = false;
            this.dgvlist.ReadOnly = true;
            this.dgvlist.RowHeadersWidth = 10;
            this.dgvlist.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        }

        private void Rent12_Load(object sender, EventArgs e)
        {
            setcontrol();
            try
            {
                cmbxl.DataSource = Cls_Func.GetBaseData("C02");
                cmbxl.DisplayMember = "DataName";
                cmbxl.ValueMember = "DataType";
                DataSet dsdepte = Dal.sys.Sys_Getdept();
                DataTable dtdp=dsdepte.Tables[0].Copy();
                DataRow dr = dtdp.NewRow();
                dr["cmn_deptcode"] = "";
                dr["cmn_dept_mc"] = "";
                dtdp.Rows.InsertAt(dr,0);
                cboDepartmentIdQ.DataSource = dtdp;
                cboDepartmentIdQ.DisplayMember = "cmn_dept_mc";
                cboDepartmentIdQ.ValueMember = "cmn_deptcode";

                cboDepartmentId.DataSource = dsdepte.Tables[0].Copy();
                cboDepartmentId.DisplayMember = "cmn_dept_mc";
                cboDepartmentId.ValueMember = "cmn_deptcode";

                DataTable dtjs = Dal.sys.Sys_Getroid().Tables[0];
                DataRow dr1 = dtjs.NewRow();
                dr1["role_Id"] = "";
                dr1["role_MC"] = "";
                dtjs.Rows.InsertAt(dr1, 0);
                cbxjs.DataSource = dtjs;
                cbxjs.DisplayMember = "role_MC";
                cbxjs.ValueMember = "role_Id";
              

                querydata();
            }
            catch (Exception ex)
            {

                pulic.Cls_Popup.MsgErrBox(ex.ToString());
            }
        }
        DataTable createTable(string strTablename)
        {
            DataTable dt = new DataTable(strTablename);
            dt.Columns.Add("EMPLOYEE_ID");
            dt.Columns.Add("EMPLOYEE_MC");
            dt.Columns.Add("DEPTCODE");
            dt.Columns.Add("ROLE_ID");
            dt.Columns.Add("SEX");
            dt.Columns.Add("EDUCATION");
            dt.Columns.Add("IN_DATE");
            dt.Columns.Add("BIRTHDAY");
            dt.Columns.Add("ADDRESS");
            dt.Columns.Add("TEL");
            dt.Columns.Add("BZ");
            dt.Columns.Add("ACTIVEFLAG");
            dt.Columns.Add("PWD");
            dt.Columns.Add("ACTIVELOGIN");
            dt.Columns.Add("PYM");
            dt.Columns.Add("WBM");
            dt.Columns.Add("SFZ");
            return dt;

        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
             try
            {

                if (!check())
                {
                    return;

                }
            DataTable dt = createTable("SYS_EMPLYEE");
            DataRow dr = dt.NewRow();
            dr["EMPLOYEE_ID"] = txtEmployeeId.Text;
            dr["EMPLOYEE_MC"] = txtEmployeeName.Text;
            dr["DEPTCODE"] = cboDepartmentId.SelectedValue;
            dr["ROLE_ID"] = cbxjs.SelectedValue;
            dr["SEX"] = cbxxb.Text;
            dr["EDUCATION"]=cmbxl.SelectedValue;
            dr["IN_DATE"]=dtpIN.Text;
            dr["BIRTHDAY"]=dtpcsrq.Text;
            dr["ADDRESS"] = txtaddress.Text.Trim();
            dr["TEL"] = txttel.Text.Trim();
            dr["BZ"] = txtbz.Text.Trim();
            dr["ACTIVEFLAG"] = (ckbzj.Checked ? "1" : "0"); ;
            dr["PWD"]="";
            dr["ACTIVELOGIN"] = (ckblog.Checked?"1":"0");
            dr["PYM"] = pulic.Cls_Func.GetPyCode(txtEmployeeName.Text);
            dr["WBM"] = pulic.Cls_Func.GetWbm(txtEmployeeName.Text);
            dr["SFZ"] = pulic.Cls_Func.GetWbm( this.txtIdentId.Text);
            dt.Rows.Add(dr);
            string strXml = pulic.XmlDataService.ToString(dt);
            DataSet dsResult = Dal.sys.Sys_employee_save(strXml, "0");

            querydata();
            clear();
            if (dsResult.Tables[0].Rows[0]["rst"].ToString().ToUpper() == "T")
            {
                pulic.Cls_Popup.MsgBox("新增成功！");
            }
            else
            {
                pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
            }
            }
             catch (Exception ex)
             {

                 pulic.Cls_Popup.MsgErrBox(ex.ToString());
             }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (pulic.Cls_Popup.MsgYesNoBox("请确认是否修改！"))
            {

                if (txtEmployeeId.ReadOnly)
                {
                    try
                    {
                        if (!check())
                        {
                            return;

                        }
                        DataTable dt = createTable("SYS_EMPLYEE");
                        DataRow dr = dt.NewRow();
                        dr["EMPLOYEE_ID"] = txtEmployeeId.Text;
                        dr["EMPLOYEE_MC"] = txtEmployeeName.Text;
                        dr["DEPTCODE"] = cboDepartmentId.SelectedValue;
                        dr["ROLE_ID"] = cbxjs.SelectedValue;
                        dr["SEX"] = cbxxb.Text;
                        dr["EDUCATION"] = cmbxl.SelectedValue;
                        dr["IN_DATE"] = dtpIN.Text;
                        dr["BIRTHDAY"] = dtpcsrq.Text;
                        dr["ADDRESS"] = txtaddress.Text.Trim();
                        dr["TEL"] = txttel.Text.Trim();
                        dr["BZ"] = txtbz.Text.Trim();
                        dr["ACTIVEFLAG"] = (ckbzj.Checked ? "1" : "0"); ;
                        dr["PWD"] = "";
                        dr["ACTIVELOGIN"] = (ckblog.Checked ? "1" : "0");
                        dr["PYM"] = pulic.Cls_Func.GetPyCode(txtEmployeeName.Text);
                        dr["WBM"] = pulic.Cls_Func.GetWbm(txtEmployeeName.Text);
                        dr["SFZ"] = pulic.Cls_Func.GetWbm(this.txtIdentId.Text);
                        dt.Rows.Add(dr);
                        string strXml = pulic.XmlDataService.ToString(dt);
                        DataSet dsResult = Dal.sys.Sys_employee_save(strXml, "1");
                        querydata();
                        clear();
                        if (dsResult.Tables[0].Rows[0]["rst"].ToString().ToUpper() == "T")
                        {
                            pulic.Cls_Popup.MsgBox("修改成功！");
                        }
                        else
                        {
                            pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
                        }
                    }
                    catch (Exception ex)
                    {

                        pulic.Cls_Popup.MsgErrBox(ex.ToString());
                    }
                }
            }
            else
            {
                pulic.Cls_Popup.MsgBox("请先新增！");
            }
        }
        //void clear()
        //{
        //    txtEmployeeId.ReadOnly = false;
        //    txtEmployeeName.Text = "";
        //    cboDepartmentId.SelectedIndex =0;
        //    cbxjs.SelectedIndex = 0;
        //    cbxxb.SelectedIndex = 0;
        //    dtpIN.Value = DateTime.Now;
        //    dtpcsrq.Value = DateTime.Now;
        //    cmbxl.SelectedIndex = 0;
        //    txtaddress.Text = "";
        //    txttel.Text = "";
        //    txtbz.Text = "";
        //    ckbzj.Checked = true;
        //    ckblog.Checked = false;
        //}

        private void btnDelete_Click(object sender, EventArgs e)
        {

            DataTable dt = createTable("SYS_EMPLYEE");
            DataRow dr = dt.NewRow();
            dr["EMPLOYEE_ID"] = txtEmployeeId.Text;
            dr["EMPLOYEE_MC"] = txtEmployeeName.Text;
            dr["DEPTCODE"] = cboDepartmentId.Text;
            dr["ROLE_ID"] = cbxjs.SelectedValue;
            dr["SEX"] = cbxxb.Text;
            dr["EDUCATION"] = cmbxl.Text;
            dr["IN_DATE"] = dtpIN.Text;
            dr["BIRTHDAY"] = dtpcsrq.Text;
            dr["ADDRESS"] = txtaddress.Text.Trim();
            dr["TEL"] = txttel.Text.Trim();
            dr["BZ"] = txtbz.Text.Trim();
            dr["ACTIVEFLAG"] = (ckbzj.Checked ? "1" : "0"); ;
            dr["PWD"] = "";
            dr["ACTIVELOGIN"] = (ckblog.Checked ? "1" : "0");
            dr["PYM"] = pulic.Cls_Func.GetPyCode(txtEmployeeName.Text);
            dr["WBM"] = pulic.Cls_Func.GetWbm(txtEmployeeName.Text);
            dt.Rows.Add(dr);
            string strXml = pulic.XmlDataService.ToString(dt);
            DataSet dsResult = Dal.sys.Sys_employee_save(strXml, "2");
            querydata();
            clear();
            if (dsResult.Tables[0].Rows[0]["rst"].ToString().ToUpper() == "T")
            {
                pulic.Cls_Popup.MsgBox("停用成功！");
            }
            else
            {
                pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
            }
        }

        private void dgvlist_Click(object sender, EventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {
                
                txtEmployeeName.Text = dgvlist.CurrentRow.Cells["colmc"].Value.ToString();
                txtEmployeeId.Text = dgvlist.CurrentRow.Cells["coldm"].Value.ToString();
                dtpIN.Value =  Convert.ToDateTime(dgvlist.CurrentRow.Cells["colrzrq"].Value);
                if (dgvlist.CurrentRow.Cells["colxb"].Value.ToString().Trim()=="男")
                {
                    cbxxb.SelectedIndex = 0;
                }
                else
                {
                     cbxxb.SelectedIndex = 1;
                }
                dtpcsrq.Value = Convert.ToDateTime(dgvlist.CurrentRow.Cells["colcsrq"].Value);
                cmbxl.SelectedValue = dgvlist.CurrentRow.Cells["colxlid"].Value;
                txtIdentId.Text = dgvlist.CurrentRow.Cells["colsfz"].Value.ToString();
                cboDepartmentId.SelectedValue = dgvlist.CurrentRow.Cells["colbmid"].Value.ToString();
                cbxjs.SelectedValue = dgvlist.CurrentRow.Cells["coljsid"].Value.ToString();
                txtaddress.Text = dgvlist.CurrentRow.Cells["coldz"].Value.ToString();
                txttel.Text = dgvlist.CurrentRow.Cells["coldh"].Value.ToString();
                ckbzj.Checked = dgvlist.CurrentRow.Cells["colztid"].Value.ToString()=="1"?true:false;
                ckblog.Checked = dgvlist.CurrentRow.Cells["collogid"].Value.ToString() == "1" ? true : false;
                txtbz.Text = dgvlist.CurrentRow.Cells["colbz"].Value.ToString();
                btnUpdate.Enabled = true;
                txtEmployeeId.ReadOnly = true;
                btnAdd.Enabled = false;
            }
        }
        void clear()
        {
            txtEmployeeIdQ.Text = "";
            txt_EmployeeNameQ.Text = "";           
            cboDepartmentIdQ.SelectedIndex = 0;
            //-------------
        
            txtEmployeeName.Text = "";
            dtpIN.Value = DateTime.Now;
            cbxxb.SelectedIndex = 0;
            txtEmployeeId.Text = "";
            dtpcsrq.Value = DateTime.Now;
            cmbxl.SelectedIndex = 0;
            txtIdentId.Text = "";
            cboDepartmentId.SelectedIndex = 0;
            cbxjs.SelectedIndex = 0;
            txtaddress.Text = "";
            txttel.Text = "";
            ckbzj.Checked = true;
            ckblog.Checked = false;
            txtbz.Text = "";
            //-------------
            txtEmployeeId.ReadOnly = false;
            btnAdd.Enabled = true;
            btnUpdate.Enabled = false;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void dgvlist_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {
                if (dgvlist.Columns[e.ColumnIndex].Name == "colbtnreset")
                {

                    Dal.sys.Sys_Setpwd("", dgvlist.CurrentRow.Cells["coldm"].Value.ToString());

                    pulic.Cls_Popup.MsgBox("重置成功！");
                    
                }
            }
        }
        bool check()
        {
            if (this.txtEmployeeId.Text.Trim().Length == 0)
            {

                pulic.Cls_Popup.MsgBox("代码不能为空！");
                return false;
            }

            if (this.txtEmployeeName.Text.Trim().Length == 0)
            {

                pulic.Cls_Popup.MsgBox("名称不能为空！");
                return false;
            }
            return true;
        }

    }
}
